package com.china08.yxyapi.api.comm.sws;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.util.SwsStatisticsUtil;

import io.swagger.annotations.Api;

@Api(tags = { "V31", "网站统计日期" })
@CrossOrigin
@RestController
public class SwsTestController extends YxyApiBaseController {

	@RequestMapping(value = "/api_yw/comm/sws/test/srh/{days}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void execute(@PathVariable(value = "days", required = false) Integer days) {
	    ArrayList<Integer> dArrayList = getOldDate(days);
	    for (int i = 0; i < dArrayList.size(); i++) {
	        System.out.println(dArrayList.get(i));
	        SwsStatisticsUtil.lectureLanMuMessageStatis(dArrayList.get(i));
	        SwsStatisticsUtil.lecturePvStatis(dArrayList.get(i));
	        SwsStatisticsUtil.lectureZiXunStatis(dArrayList.get(i));
	        SwsStatisticsUtil.lectureLanMuStatis(dArrayList.get(i));
        }
	}

	protected static class ReqModel extends YxyApiReqModel {
	    //获取过去多少天
        public Integer days;
	}

	protected static class RespModel {

	}
	// 获取过去N天的时间
    public static ArrayList<Integer> getOldDate(int intervals) {
        ArrayList<Integer> pastDaysList = new ArrayList<>();
        for (int i = intervals-1; i >= 0 ; i--) {
            pastDaysList.add(Integer.parseInt(getPastDate(i)));
        }
        return pastDaysList;
    }

    // 获取过去N天的时间
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result = format.format(today);
        return result;
    }
}
